﻿<!-- Views/Admin/ExamResults.cshtml -->
@model List<WebApplication1.Models.ExamResult>

@{
    ViewBag.Title = WebApplication1.Utilities.LanguageManager.GetText("ExamResults");
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">
                            @WebApplication1.Utilities.LanguageManager.GetText("ExamResults")
                        </h5>
                    </div>
                </div>
                <div class="card-body">
                    <!-- 筛选表单 -->
                    <div class="row mb-3">
                        <div class="col-md-12">
                            @using (Html.BeginForm("ExamResults", "Admin", FormMethod.Get, new { @class = "row g-3" }))
                            {
                                <div class="col-md-3">
                                    <label class="form-label">@WebApplication1.Utilities.LanguageManager.GetText("StartDate")</label>
                                    <input type="date" name="startDate" class="form-control" value="@ViewBag.StartDate" />
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">@WebApplication1.Utilities.LanguageManager.GetText("EndDate")</label>
                                    <input type="date" name="endDate" class="form-control" value="@ViewBag.EndDate" />
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">@WebApplication1.Utilities.LanguageManager.GetText("PassStatus")</label>
                                    <select name="passStatus" class="form-control">
                                        <option value="">@WebApplication1.Utilities.LanguageManager.GetText("All")</option>
                                        <option value="passed" @(ViewBag.PassStatus == "passed" ? "selected" : "")>@WebApplication1.Utilities.LanguageManager.GetText("Passed")</option>
                                        <option value="failed" @(ViewBag.PassStatus == "failed" ? "selected" : "")>@WebApplication1.Utilities.LanguageManager.GetText("Failed")</option>
                                    </select>
                                </div>
                                <div class="col-md-3 align-self-end">
                                    <button type="submit" class="btn btn-primary me-2">
                                        <i class="glyphicon glyphicon-search"></i> @WebApplication1.Utilities.LanguageManager.GetText("Search")
                                    </button>
                                    <a href="@Url.Action("ExportExamResultsToExcel", "Admin")" class="btn btn-success" id="exportBtn">
                                        <i class="glyphicon glyphicon-export"></i> @WebApplication1.Utilities.LanguageManager.GetText("ExportToExcel")
                                    </a>
                                </div>
                            }
                        </div>
                    </div>

                    @if (TempData["SuccessMessage"] != null)
                    {
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            @TempData["SuccessMessage"]
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    }

                    @if (TempData["ErrorMessage"] != null)
                    {
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            @TempData["ErrorMessage"]
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    }

                    @if (Model.Count == 0)
                    {
                        <div class="alert alert-info text-center">
                            @WebApplication1.Utilities.LanguageManager.GetText("NoExamResults")
                        </div>
                    }
                    else
                    {
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead class="table-dark">
                                    <tr>
                                        <th>@WebApplication1.Utilities.LanguageManager.GetText("ExamName")</th>
                                        <th>@WebApplication1.Utilities.LanguageManager.GetText("UserName")</th>
                                        <th>@WebApplication1.Utilities.LanguageManager.GetText("StartTime")</th>
                                        <th>@WebApplication1.Utilities.LanguageManager.GetText("Score")</th>
                                        <th>@WebApplication1.Utilities.LanguageManager.GetText("TotalScore")</th>
                                        <th>@WebApplication1.Utilities.LanguageManager.GetText("PassScore")</th>
                                        <th>@WebApplication1.Utilities.LanguageManager.GetText("Status")</th>
                                        <th>@WebApplication1.Utilities.LanguageManager.GetText("Duration") (@WebApplication1.Utilities.LanguageManager.GetText("Minutes"))</th>
                                        <th width="15%">@WebApplication1.Utilities.LanguageManager.GetText("Action")</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var result in Model)
                                    {
                                        <tr>
                                            <td>@result.Exam.ExamName</td>
                                            <td>@result.User.FullName</td>
                                            <td>@result.StartTime.ToString("yyyy-MM-dd HH:mm")</td>
                                            <td><span class="badge bg-info">@result.Score</span></td>
                                            <td>@result.TotalScore</td>
                                            <td>@result.Exam.PassScore</td>
                                            <td>
                                                @if (result.Score >= result.Exam.PassScore)
                                                {
                                                    <span class="badge bg-success">@WebApplication1.Utilities.LanguageManager.GetText("Passed")</span>
                                                }
                                                else
                                                {
                                                    <span class="badge bg-danger">@WebApplication1.Utilities.LanguageManager.GetText("Failed")</span>
                                                }
                                            </td>
                                            <td>
                                                @{

                                                    var isPassed = result.Score >= result.Exam.PassScore;

                                                    <!-- 使用新方法计算考试时长 -->
                                                    {
                                                        string durationStr = "0.00";
                                                        if (result.EndTime != null && result.StartTime != null)
                                                        {
                                                            try
                                                            {
                                                                // 新的计算方法：使用 TotalMinutes 属性直接获取分钟数
                                                                TimeSpan durationSpan = ((DateTime)result.EndTime).Subtract((DateTime)result.StartTime);
                                                                durationStr = durationSpan.TotalMinutes.ToString("F2");
                                                            }
                                                            catch
                                                            {
                                                                durationStr = "0.00";
                                                            }
                                                          
                                                            ViewBag.durationStr = durationStr;

                                                        }
                                                    }
                                                }
                                                @ViewBag.durationStr
                                            </td>
                                            <td>
                                                <a href="@Url.Action("ExamResultDetail", "Admin", new { id = result.ResultId })"
                                                   class="btn btn-sm btn-outline-primary">
                                                    @WebApplication1.Utilities.LanguageManager.GetText("ViewDetails")
                                                </a>

                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</div>

<script src="~/Scripts/jquery-3.6.0.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>

<script>
    $(document).ready(function () {
        // 关闭警告框
        $('.alert .btn-close').click(function () {
            $(this).parent('.alert').fadeOut();
        });

        // 自动隐藏成功消息
        setTimeout(function () {
            $('.alert-success').fadeOut();
        }, 5000);

        // 导出按钮点击事件
        $('#exportBtn').click(function (e) {
            e.preventDefault();

            // 获取筛选参数
            var startDate = $('input[name="startDate"]').val();
            var endDate = $('input[name="endDate"]').val();
            var passStatus = $('select[name="passStatus"]').val();

            // 构建导出URL
            var exportUrl = '@Url.Action("ExportExamResultsToExcel", "Admin")';
            if (startDate || endDate || passStatus) {
                exportUrl += '?';
                var params = [];
                if (startDate) params.push('startDate=' + startDate);
                if (endDate) params.push('endDate=' + endDate);
                if (passStatus) params.push('passStatus=' + passStatus);
                exportUrl += params.join('&');
            }

            // 执行导出
            window.location.href = exportUrl;
        });
    });
</script>